package com.truckdispatching.util;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtil {

	private static SharedPreferencesUtil instance;

	private SharedPreferences mPreference;

	private Application mApplication;

	public static synchronized SharedPreferencesUtil getInstance() {
		if (instance == null) {
			instance = new SharedPreferencesUtil();
		}
		return instance;
	}

	/**
	 * 用户名
	 */
	private final String KEY_USERNAME = "login_username";

	public void setUsername(String username) {
		putString(KEY_USERNAME, username);
	}

	public String getUsername() {
		return getString(KEY_USERNAME, "");
	}

	/**
	 * 密码
	 */
	private final String KEY_PASSWORD = "login_password";

	public void setPassword(String password) {
		putString(KEY_PASSWORD, password);
	}

	public String getPassword() {
		return getString(KEY_PASSWORD, "");
	}

	/**
	 * 上次发送push的时间
	 */
	private final String KEY_LAST_PUSH_TIME = "last_pus_timeh";

	public void setLastPushTime(long timestamep) {
		putLong(KEY_LAST_PUSH_TIME, timestamep);
	}

	public long getLastPushTime() {
		return getLong(KEY_LAST_PUSH_TIME, 0);
	}

	/**
	 * 初始化，预加载数据
	 */
	public void initWithApplication(Application application) {
		mApplication = application;
		if (mPreference == null) {
			mPreference = mApplication.getSharedPreferences("truckdispatching", Context.MODE_PRIVATE);
		}
	}

	public void putString(String key, String value) {
		if (mPreference != null) {
			mPreference.edit().putString(key, value).commit();
		}
	}

	public String getString(String key, String defValue) {
		if (mPreference != null) {
			return mPreference.getString(key, defValue);
		} else {
			return defValue;
		}
	}

	public void putInt(String key, int value) {
		if (mPreference != null) {
			mPreference.edit().putInt(key, value).commit();
		}
	}

	public int getInt(String key, int defValue) {
		if (mPreference != null) {
			return mPreference.getInt(key, defValue);
		} else {
			return defValue;
		}
	}

	public void putLong(String key, long value) {
		if (mPreference != null) {
			mPreference.edit().putLong(key, value).commit();
		}
	}

	public long getLong(String key, long defValue) {
		if (mPreference != null) {
			return mPreference.getLong(key, defValue);
		} else {
			return defValue;
		}
	}

	public void putFloat(String key, float value) {
		if (mPreference != null) {
			mPreference.edit().putFloat(key, value).commit();
		}
	}

	public float getFloat(String key, float defValue) {
		if (mPreference != null) {
			return mPreference.getFloat(key, defValue);
		} else {
			return defValue;
		}
	}

	public void putBoolean(String key, boolean value) {
		if (mPreference != null) {
			mPreference.edit().putBoolean(key, value).commit();
		}
	}

	public boolean getBoolean(String key, boolean defValue) {
		if (mPreference != null) {
			return mPreference.getBoolean(key, defValue);
		} else {
			return defValue;
		}
	}

	public void remove(String key) {
		if (mPreference != null) {
			mPreference.edit().remove(key).commit();
		}
	}
}
